/**
 * Copyright (C) 2014 xspace app Project.
 */

package cn.xspace.module.other.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Timer;
import java.util.TimerTask;

import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

/**
 * @ClassName: VedioPlayUtil
 * @author:guankui
 * @Date：2014年5月8日
 * @version  V1.0.0
 * @Description: 音频播放
 */
public class VedioPlayUtil extends MediaPlayer{

	
	private Timer mTimer = null;// 语音动画控制器

	private TimerTask mTimerTask = null;	// 语音动画控制任务
	
	private int index = 1;// 记录语音动画图片
	public static VedioPlayUtil mMediaPlayer;
	AudioAnimationHandler audioAnimationHandler = null;
	
	public static VedioPlayUtil getInstance(){
		if(mMediaPlayer==null){
			mMediaPlayer=new VedioPlayUtil();
		}
		
		return mMediaPlayer;	
	}

	/**
	 * 播放语音图标动画
	 */
	public void playAudioAnimation(final String name, final ImageView imageView) {
		// 定时器检查播放状态

		stopTimer();
		mTimer = new Timer();
		
		// 将要关闭的语音图片归位
		if (audioAnimationHandler != null) {
			Message msg = new Message();
			msg.what = 3;
			audioAnimationHandler.sendMessage(msg);
		}

		audioAnimationHandler = new AudioAnimationHandler(imageView);
		mTimerTask = new TimerTask() {

			@Override
			public void run() {
				try {
					if (mMediaPlayer.isPlaying()) {
						index = (index + 1) % 3;

						Message msg = new Message();
						msg.what = index;
						audioAnimationHandler.sendMessage(msg);
					}

					File file = new File(name);
					FileInputStream fis = new FileInputStream(file);
					mMediaPlayer.setDataSource(fis.getFD());
					mMediaPlayer.prepare();
					mMediaPlayer.start();

					mMediaPlayer
							.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
								public void onCompletion(MediaPlayer mp) {
									if (mMediaPlayer != null) {
										mMediaPlayer.stop();
										mMediaPlayer.release();
										mMediaPlayer = null;

										Message msg = new Message();
										msg.what = 3;
										audioAnimationHandler.sendMessage(msg);
										audioAnimationHandler = null;
										stopTimer();
									}
								}
							});

				} catch (Exception e) {
					
				}

			}
		};
		// 调用频率为500毫秒一次
		mTimer.schedule(mTimerTask, 0, 500);

	}

	public class AudioAnimationHandler extends Handler {
		ImageView imageView;
		// 判断是左对话框还是右对话框
		boolean isleft;

		public AudioAnimationHandler(ImageView imageView) {
			this.imageView = imageView;
			// 判断是左对话框还是右对话框 我这里是在前面设置ScaleType来表示的

		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			// 根据msg.what来替换图片，达到动画效果
			switch (msg.what) {
			case 0:
				if (audioAnimationHandler == null) {
					break;
				}
				imageView.setImageResource(RProxy.speaker_0);
				break;
			case 1:
				if (audioAnimationHandler == null) {
					break;
				}
				imageView.setImageResource(RProxy.speaker_1);
				break;
			case 2:
				if (audioAnimationHandler == null) {
					break;
				}
				imageView.setImageResource(RProxy.speaker_2);
				break;
			case 3:
				imageView.setImageResource(RProxy.speaker_3);
				break;
			}
		}

	}
	/**
	 * 停止
	 */
	public void stopTimer() {
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}

		if (mTimerTask != null) {
			mTimerTask.cancel();
			mTimerTask = null;
		}

	}
	public static void stopVedio(){
		if(mMediaPlayer!=null){
			mMediaPlayer.stop();
			mMediaPlayer=null;
		}
	}
}
